package com.vue.power.backend.sys.service;

import com.github.pagehelper.PageHelper;
import com.vue.power.backend.common.base.constant.SystemStaticConst;
import com.vue.power.backend.common.base.dao.BaseDao;
import com.vue.power.backend.common.base.entity.Page;
import com.vue.power.backend.common.base.service.BaseService;
import com.vue.power.backend.common.util.date.DateUtil;
import com.vue.power.backend.common.util.page.PageUtil;
import com.vue.power.backend.config.redis.RedisCache;
import com.vue.power.backend.sys.dao.RoleAssociateTreeDao;
import com.vue.power.backend.sys.dao.RoleTreeDao;
import com.vue.power.backend.sys.entity.RoleAssociateTree;
import com.vue.power.backend.sys.entity.RoleTree;
import com.vue.power.backend.sys.entity.User;
import com.vue.power.backend.sys.mapper.RoleTreeMapper;
import com.vue.power.backend.sys.util.TreeInstall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleTreeService extends BaseService<RoleTree> {

    @Autowired
    private RoleTreeDao roleTreeDao;
    @Autowired
    private RoleAssociateTreeDao roleAssociateTreeDao;
    @Autowired
    private RoleTreeMapper roleTreeMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    protected BaseDao<RoleTree> getDao() {
        return this.roleTreeDao;
    }

    /**
     * 功能描述：更新按钮节点状态
     * @param entity
     * @return
     */
    public boolean operateTreeButton(RoleTree entity){
        return roleTreeDao.operateTreeButton(entity)>0;
    }


    @Override
    public boolean delete(RoleTree entity) throws Exception {
        RoleAssociateTree roleAssociateTree = new RoleAssociateTree();
        roleAssociateTree.setTreeId(entity.getTreeId());
        roleAssociateTreeDao.delete(roleAssociateTree);
        return super.delete(entity);
    }

    @Override
    public boolean save(RoleTree entity) throws Exception {
        entity.setState("1");
        entity.setCrtDate(DateUtil.currentMillitm());
        User user = getUser(redisCache);
        entity.setCrtUser(user.getUserName());
        entity.setCrtUserId(user.getUserId());
        return super.save(entity);
    }

    /**
     * 功能描述：获取所有的菜单数据
     * @return
     */
    public Map<String,Object> getAllTreeList(boolean justTree){
        Map<String,Object> result = new HashMap<String, Object>();
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG,"获取所有的菜单数据成功！");
        RoleTree query = new RoleTree();
        if(justTree){
            query.setType("1");
        }
        query.setState("1");
        PageHelper.startPage(1, 9999);
        result.put(SystemStaticConst.ENTITY,TreeInstall.installTree(roleTreeMapper.treesToTreeDTO(roleTreeDao.select(query))));
        return result;
    }


}
